### Copyright (C) 2010 Desmond Carter
###
### This file is part of Ar.
###
### Ar is free software: you can redistribute it and/or modify it under the
### terms of the GNU General Public License as published by the Free Software
### Foundation, either version 3 of the License, or (at your option) any later
### version.
###
### Ar is distributed in the hope that it will be useful, but WITHOUT ANY
### WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
### FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
### details.
###
### You should have received a copy of the GNU General Public License along
### with Ar. If not, see <http://www.gnu.org/licenses/>.

## This file is for the class BoardSegment, which is a glorified special case
## of the ArBoard constructor.


from ar.locals import *
from arboard import ArBoard
from axis import Axis
import ar
import ar.drawtools as drawtools


class BoardSegment(ArBoard):
    '''This is basically just a special constructor for boards that are subsets
    of others.
    dimensions should be a list of integers; lengths is an equal-size list of
    lists of the form [min, max].'''

    def __init__(self, parent, dimensions, lengths=[]):
        
        # This copies the axis list; aliasing it would be very bad.
        # In the next version (or whenever I get around to it), this should be
        # replaced with "[axis.copy() for axis in parent.axes]" or somesuch.
        # Update: implemented.
        axes = [axis.copy()
                for axis in
                [parent.axes[i] for i in dimensions]]

        # Make sure ArBoard.axes[i] is always an axis of order i. There are
        # problems with this that need to be addressed, but I cannot describe
        # them succinctly (which is probably bad ;D).
        # Update: the problem is that the layout of the board changes depending
        # on the order of the entries in ar.drawn_dimensions. The way to fix
        # this is probably to change the draw functions to treat it as a set
        # rather than a sequence, or possibly even to keep it ordered at all
        # times.
        for i, axis in enumerate(axes):
            axis.order = i

        if lengths:
            axes = map(Axis.truncate, axes, *[[list[i] for list in lengths]
                                              for i in (0, 1)])

        # This looks really messy and hard on performance, but I'll need more
        # infrastructure to fix it. I'll see what I can do on the next pass.
        piece_tester = ArBoard(axes)
        for piece in parent.pieces:
            if piece_tester.contains(piece):
                piece_tester.pieces.append(piece)

        ArBoard.__init__(self, axes, piece_tester.pieces[:])
